﻿using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
using System.IO;
using Microsoft.VisualBasic.FileIO;
using AMS.DeDuplicator.CommonLayer;

namespace AMS.DeDuplicator.BusinessLayer.Command
{
    public class CreateShortcutCommand : Command
    {
        private string _targetFilePath;
        private string _destinationDirectoryPath;
        private string _newName;

        #region Constructors
        public CreateShortcutCommand(string targetFilePath, string destinationDirectoryPath, string newName)
            : base()
        {
            InputValidator.ValidateNotNull(targetFilePath);
            InputValidator.ValidateFileExists(targetFilePath);
            _targetFilePath = targetFilePath;

            InputValidator.ValidateDirectoryExists(_destinationDirectoryPath);
            _destinationDirectoryPath = destinationDirectoryPath;

            InputValidator.ValidateNotNull(_newName);
            if (newName.Contains("\\"))
                ExceptionHandler.InvalidFilenameException();
            _newName = newName;
        }
        #endregion

        public override bool Execute()
        {
            if (this.Executed)
                ExceptionHandler.AlreadyExecutedException();

            try
            {
                ExceptionHandler.NotImplementedException();

                // create the shortcut // needs work!
                using (StreamWriter writer = new StreamWriter(_destinationDirectoryPath + "\\" + _newName + ".url"))
                {
                    writer.WriteLine("[InternetShortcut]");
                    writer.WriteLine("URL=file:///" + _targetFilePath);
                    writer.WriteLine("IconIndex=0");
                    writer.Flush();
                }
            }
            catch (Exception)
            {
                this.Executed = !File.Exists(_targetFilePath);
                return this.Executed;
            }

            this.Executed = true;
            return true;
        }

        //public override bool Execute(object parameter)
        //{
        //    _targetFilePath = parameter as string;
        //    return this.Execute();
        //}

        public override bool Undo()
        {
            if (!Executed)
                ExceptionHandler.NotExecutedException();

            // delete the shortcut
            return false;
        }
    }
}
